package gwtI18nEditor.widgets;

import java.awt.BorderLayout;
import java.awt.Component;

import gwtI18nEditor.datamodel.Message;
import gwtI18nEditor.datamodel.MessageFile;
import gwtI18nEditor.resources.ImageLibrary;

import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.border.Border;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

@SuppressWarnings("serial")
public abstract class MessagesListing extends JPanel implements ListSelectionListener, ListCellRenderer {
	
	private static final DefaultListCellRenderer RENDERER = new DefaultListCellRenderer();
	
	private Message lastSelection = null;
	private JLabel  caption;
	private JList   list;

	public MessagesListing() {
		super(new BorderLayout());
		Border padding = BorderFactory.createEmptyBorder(3,2,3,2);
		caption = new JLabel(ImageLibrary.get("message_file.png"), JLabel.LEFT);
		caption.setBorder(padding);
		add(caption, BorderLayout.PAGE_START);
		list = new JList();
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.addListSelectionListener(this);
		list.setCellRenderer(this);
		add(new JScrollPane(list), BorderLayout.CENTER);
	}
	
	public void updateList(MessageFile messages) {
		caption.setText(messages.getPackage()+"."+messages.getIdentifier());
		list.setListData(messages.getMessages());
	}
	
	public void previousMessage() {
		list.setSelectedIndex(list.getSelectedIndex()-1);
		list.ensureIndexIsVisible(list.getSelectedIndex());
	}
	
	public void nextMessage() {
		list.setSelectedIndex(list.getSelectedIndex()+1);
		list.ensureIndexIsVisible(list.getSelectedIndex());
	}

	@Override
	public void valueChanged(ListSelectionEvent event) {
		Object entry = list.getSelectedValue();
		if ((entry==null) || !(entry instanceof Message)) return;
		Message selection = (Message) entry;
		if (selection==lastSelection) return;
		lastSelection=selection;
		onSelect(selection);
	}

	@Override
	public Component getListCellRendererComponent(JList list, Object value,	int index, boolean isSelected, boolean cellHasFocus) {
		JLabel result = (JLabel) RENDERER.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
		if ((value==null) || !(value instanceof Message)) return result;
		Message message = (Message) value;
		OverlayIcon icon;
		switch (message.getType()) {
			case SAFEHTML: 
				icon = new OverlayIcon(ImageLibrary.get("safehtml_message.png"));
				break;
			case STRING:
				icon = new OverlayIcon(ImageLibrary.get(  "string_message.png"));
				break;
			default:
				icon = new OverlayIcon(ImageLibrary.get( "unknown_message.png"));
		}
		if (message.needsAttention()) icon.setOverlay(ImageLibrary.get("attention.png"));
		result.setIcon(icon);
		return result;
	}

	public abstract void onSelect(Message message);
}
